<?php

declare(strict_types=1);

namespace App\Mall\Model;

use App\Fastapp\Model\FileTrait;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Database\Model\SoftDeletes;
use Mine\MineModel;

/**
 * @property int $id 主键
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $deleted_at
 * @property array $name 名称
 * @property array $title 标题
 * @property array $images 展示图
 * @property string $no 商品编码
 * @property int $shop_id shop_id
 * @property int $is_spec is_spec
 * @property int $check_stock 检测库存
 * @property int $is_virtual 虚拟商品
 * @property int $is_arrival 到店
 * @property int $online 上架
 * @property int $buy_limit 限购数
 * @property int $is_integral 积分
 * @property int $comments 评论数
 * @property int $views 浏览数
 * @property int $collects 收藏数
 * @property int $sort 排序
 * @property string $costprice 成本价
 * @property string $mktprice 市场价
 * @property int $tmp_id 展示模版
 * @property int $brand_id 品牌
 * @property array $content 商品介绍
 * @property-read null|\Hyperf\Database\Model\Collection|MallGoodsCategory[] $category
 * @property-read null|\Hyperf\Database\Model\Collection|MallGoodsTag[] $tag
 * @property-read null|\Hyperf\Database\Model\Collection|MallGoodsSpec[] $spec
 * @property-read null|MallBrand $brand
 */
class MallGoods extends MineModel
{
    use SoftDeletes, FileTrait;

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'mall_goods';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'created_at', 'updated_at', 'deleted_at', 'name', 'title', 'images', 'no', 'shop_id', 'is_spec', 'check_stock', 'is_virtual', 'is_arrival', 'online', 'buy_limit', 'is_integral', 'comments', 'views', 'collects', 'sort', 'costprice', 'mktprice', 'tmp_id', 'brand_id', 'content'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'name' => 'array', 'title' => 'array', 'images' => 'array', 'shop_id' => 'integer', 'is_spec' => 'integer', 'check_stock' => 'integer', 'is_virtual' => 'integer', 'is_arrival' => 'integer', 'online' => 'integer', 'buy_limit' => 'integer', 'is_integral' => 'integer', 'comments' => 'integer', 'views' => 'integer', 'collects' => 'integer', 'sort' => 'integer', 'costprice' => 'decimal:2', 'mktprice' => 'decimal:2', 'tmp_id' => 'integer', 'brand_id' => 'integer', 'content' => 'array'];

    public function category(): HasMany
    {
        return $this->hasMany(MallGoodsCategory::class, 'goods_id', 'id');
    }

    public function tag(): HasMany
    {
        return $this->hasMany(MallGoodsTag::class, 'goods_id', 'id');
    }

    public function shop(): HasMany
    {
        return $this->hasMany(MallGoodsShop::class, 'goods_id', 'id');
    }

    public function spec(): HasMany
    {
        return $this->hasMany(MallGoodsSpec::class, 'goods_id', 'id')->with(['price']);
    }

    public function brand(): HasOne
    {
        return $this->hasOne(MallBrand::class, 'id', 'brand_id');
    }
}
